CategoriesManagePortlet = Class.extend({
	
	init: function()	{
		this.name = "CategoriesManagePortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	onEditCategory: function(eventData)	{
		var target = eventData.event.target;
		var id = eventData.id;
		var container = $(target).parent().siblings('.category-name').find('input');
		var txt = $(container).val();
		this.onAjax('admin', 'edit-category', {id: id, name: txt}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Sửa thành công');
			}
		});
	},
	
	run: function()	{
		//store the current object
		var obj = this;
		this.onAjax('categories', 'load-all-categories', {}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = {};
				obj.model.categories = Array();
				for(var i in ret)	{
					var c = ret[i];
					if (c.id != 0)	{
						obj.model.categories.push(c);
					}
				}
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);